package com.cloudream.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtils {

    public static int toInt(Object obj) {
        return toInt(obj, -1);
    }

    public static int toInt(Object obj, int defaultValue) {
        try {
            String rtn = String.valueOf(obj);
            rtn = rtn.replaceAll("[^\\d]+", "");
            return Integer.parseInt(rtn);
        } catch (Exception e) {
            return defaultValue;
        }
    }


    public static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            String group = matcher.group(2);
            ch = (char) Integer.parseInt(group, 16);
            String group1 = matcher.group(1);
            str = str.replace(group1, ch + "");
        }
        return str;
    }

    public static String toString(Object obj) {
        if (obj == null) {
            return "";
        } else {
            return String.valueOf(obj);
        }
    }
}
